<?php

namespace common\entities\myhtsq;

use Yii;
use Exception;
use common\error\ErrorCode;

/**
 * This is the model class for table "myscrm_invoice".
 *
 * @property string $invoice_id
 * @property integer $invoice_category
 * @property integer $invoice_type
 * @property integer $is_main_invoice
 * @property string $invoice_code
 * @property string $invoice_no
 * @property string $proposer_id
 * @property string $invoice_date
 * @property string $invoice_amount
 * @property string $customer_name
 * @property string $invoice_company_id
 * @property integer $is_child_invoice
 * @property string $main_invoice_id
 * @property integer $invoice_status
 * @property string $contract_id
 * @property string $receivables_amount
 * @property string $payment_amount
 * @property integer $is_red_invoice
 * @property string $red_by_invoice_id
 * @property integer $is_lose
 * @property integer $is_receipt
 * @property integer $remark
 * @property string $created_on
 * @property string $created_by
 * @property string $modified_on
 * @property string $modified_by
 */
class InvoiceEntity extends \common\entities\ConfigEntity
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'myscrm_invoice';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['invoice_category', 'is_main_invoice', 'invoice_status', 'is_red_invoice', 'is_lose', 'is_child_invoice', 'is_receipt', 'invoice_type', 'is_havecontract'], 'integer'],
            [['invoice_date', 'created_on', 'modified_on', 'remark'], 'safe'],
            [['invoice_amount', 'receivables_amount', 'payment_amount'], 'number'],
            [['invoice_id', 'proposer_id', 'invoice_company_id', 'main_invoice_id', 'contract_id', 'created_by', 'modified_by'], 'string', 'max' => 36],
            [['invoice_code', 'invoice_no'], 'string', 'max' => 50],
            [['customer_name'], 'string', 'max' => 100]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'invoice_id' => '发票ID',
            'invoice_category' => '发票类别',
            'invoice_type' => '发票类型',
            'is_main_invoice' => '是否主发票',
            'invoice_code' => '发票代码',
            'invoice_no' => '发票编号',
            'proposer_id' => '申请人ID',
            'invoice_date' => '开票时间',
            'invoice_amount' => '开票金额',
            'customer_name' => '客户名称',
            'invoice_company_id' => '开票单位ID',
            'is_child_invoice' => '是否子发票',
            'is_havecontract' => '是否有合同',
            'main_invoice_id' => '主发票ID',
            'invoice_status' => '发票状态',
            'contract_id' => '合同ID',
            'receivables_amount' => '关联应收款金额',
            'payment_amount' => '关联回款金额',
            'is_red_invoice' => '是否红冲发票',
            'red_by_invoice_id' => '被红冲发票ID',
            'is_lose' => '是否遗失',
            'is_receipt' => '是否收据',
            'remark' => '备注',
            'created_on' => '创建时间',
            'created_by' => '创建人',
            'modified_on' => '修改时间',
            'modified_by' => '修改人'
        ];
    }

    /**
     * 检查是否存在合同id发票
     * @param string $contractId 合同id
     * @return boolean
     */
    public static function isExistContractInvoice($contractId)
    {
        return self::find()->where(['contract_id' => $contractId])->exists();
    }

    /**
     * 检查是否存在对应的发票
     * @param string $invoiceId 合同id
     * @return boolean
     */
    public static function isExistInvoice($invoiceId)
    {
        return self::find()->where(['invoice_id' => $invoiceId])->exists();
    }

    /**
     * 根据发票ID删除对应的发票信息
     * @param string $invoiceId 发票ID
     * @return boolean 返回true时表示删除成功，否则失败
     */
    public static function deleteInvoice($invoiceId)
    {
        try {
            self::deleteAll(['invoice_id' => $invoiceId]);
            return true;
        } catch (Exception $ex) {
            return self::setAndReturn(ErrorCode::ERR_INVOICE_DELETE_FAIL, $ex->getMessage());
        }
    }

    /**
     * 根据发票ID设置当前发票成遗失状态
     * @param string $invoiceId 发票ID
     * @return boolean 返回true时表示设置成功，否则失败
     */
    public static function doLose($invoiceId)
    {
        try {
            $result = self::updateAll(['is_lose' => 1, 'receivables_amount' => 0, 'payment_amount' => 0], ['invoice_id' => $invoiceId]);
            return true;
        } catch (Exception $ex) {
            return self::setAndReturn(ErrorCode::ERR_INVOICE_DOLOSE_FAIL, $ex->getMessage());
        }
    }

    /**
     * 根据发票ID获取发票信息
     * @param string $invoiceId
     * @return \common\entities\myhtsq\InvoiceEntity
     */
    public static function getInvoiceInfo($invoiceId)
    {
        $entity = self::findOne(['invoice_id' => $invoiceId]);
        return $entity;
    }

}
